package org.exez.modules.visitor.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.exez.common.api.vo.Result;
import org.exez.common.aspect.annotation.AutoLog;
import org.exez.common.system.base.controller.JeecgController;
import org.exez.common.system.vo.LoginUser;
import org.exez.modules.visitor.constant.AuthorityEnum;
import org.exez.modules.visitor.entity.PushRecord;
import org.exez.modules.visitor.service.IPushRecordService;
import org.exez.modules.visitor.util.AES;
import org.exez.modules.visitor.vo.PushRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

 /**
 * @Description: 访客推送记录
 * @Author: exez-boot
 * @Date:   2020-06-28
 * @Version: V1.0
 */
@Slf4j
@Api(tags="访客授权日志")
@RestController
@RequestMapping("/visitor/pushRecord")
public class PushRecordController extends JeecgController<PushRecord, IPushRecordService> {
	@Autowired
	private IPushRecordService pushRecordService;

	/**
	 * 分页列表查询
	 *
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@AutoLog(value = "访客授权日志")
	@ApiOperation(value="访客授权日志", notes="访客授权日志")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "deviceName", value = "设备名称", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "institutionId", value = "单位id", required = false, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageNo", value = "页码", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "页码大小", required = true, dataType = "Integer", paramType = "query"),
	})
	@GetMapping(value = "/list")
	public Result<Page<PushRecordVO>> queryPageList(@RequestParam(name="deviceName", required = false) String deviceName,
													@RequestParam(name="institutionId", required = false) Integer institutionId,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		Result<Page<PushRecordVO>> result = new Result<>();
		LambdaQueryWrapper<PushRecord> queryWrapper = new LambdaQueryWrapper<>();

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		if (null != institutionId) {
			queryWrapper.eq(PushRecord::getInstitutionId, institutionId);
		}else if (!AuthorityEnum.ONE_USER.getIndex().equals(sysUser.getAuthority())) {
			queryWrapper.in(PushRecord::getInstitutionId, AES.integerList(sysUser.getInstitutionId()));
		}


		if (StringUtils.isNotBlank(deviceName)) {
			queryWrapper.like(PushRecord::getDeviceName, deviceName);
		}

		queryWrapper.orderByDesc(PushRecord::getId);

		Page<PushRecord> page = new Page<PushRecord>(pageNo, pageSize);
		Page<PushRecordVO> pushRecordVOPage = pushRecordService.listPushRecord(page, queryWrapper);
		result.successSetDate(pushRecordVOPage);
		return result;
	}


//	/**
//	 * 添加
//	 *
//	 * @param pushRecord
//	 * @return
//	 */
//	@AutoLog(value = "访客推送记录-添加")
//	@ApiOperation(value="访客推送记录-添加", notes="访客推送记录-添加")
//	@PostMapping(value = "/add")
//	public Result<?> add(@RequestBody PushRecord pushRecord) {
//		pushRecordService.save(pushRecord);
//		return Result.ok("添加成功！");
//	}
//
//	/**
//	 * 编辑
//	 *
//	 * @param pushRecord
//	 * @return
//	 */
//	@AutoLog(value = "访客推送记录-编辑")
//	@ApiOperation(value="访客推送记录-编辑", notes="访客推送记录-编辑")
//	@PutMapping(value = "/edit")
//	public Result<?> edit(@RequestBody PushRecord pushRecord) {
//		pushRecordService.updateById(pushRecord);
//		return Result.ok("编辑成功!");
//	}
//
//	/**
//	 * 通过id删除
//	 *
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "访客推送记录-通过id删除")
//	@ApiOperation(value="访客推送记录-通过id删除", notes="访客推送记录-通过id删除")
//	@DeleteMapping(value = "/delete")
//	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
//		pushRecordService.removeById(id);
//		return Result.ok("删除成功!");
//	}
//
//	/**
//	 * 批量删除
//	 *
//	 * @param ids
//	 * @return
//	 */
//	@AutoLog(value = "访客推送记录-批量删除")
//	@ApiOperation(value="访客推送记录-批量删除", notes="访客推送记录-批量删除")
//	@DeleteMapping(value = "/deleteBatch")
//	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
//		this.pushRecordService.removeByIds(Arrays.asList(ids.split(",")));
//		return Result.ok("批量删除成功！");
//	}
//
//	/**
//	 * 通过id查询
//	 *
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "访客推送记录-通过id查询")
//	@ApiOperation(value="访客推送记录-通过id查询", notes="访客推送记录-通过id查询")
//	@GetMapping(value = "/queryById")
//	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
//		PushRecord pushRecord = pushRecordService.getById(id);
//		return Result.ok(pushRecord);
//	}
//
//  /**
//   * 导出excel
//   *
//   * @param request
//   * @param pushRecord
//   */
//  @RequestMapping(value = "/exportXls")
//  public ModelAndView exportXls(HttpServletRequest request, PushRecord pushRecord) {
//      return super.exportXls(request, pushRecord, PushRecord.class, "访客推送记录");
//  }
//
//  /**
//   * 通过excel导入数据
//   *
//   * @param request
//   * @param response
//   * @return
//   */
//  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//      return super.importExcel(request, response, PushRecord.class);
//  }

}
